package com.roocon.thread.ExceptionHandler;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolExecutorDemo {

  public static void main(String[] args) {
    //1.创建线程池
    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 10, 0L, TimeUnit.MILLISECONDS,
        new LinkedBlockingQueue<>()) {
      @Override
      protected void afterExecute(Runnable r, Throwable t) {
        if (r instanceof Thread) {
          if (t != null) {
            //处理捕获的异常
            System.out.printf("thread:" + t.getMessage());
          }
        } else if (r instanceof FutureTask) {
          FutureTask futureTask = (FutureTask) r;
          try {
            futureTask.get();
          } catch (InterruptedException e) {
            e.printStackTrace();
          } catch (ExecutionException e) {
            //处理捕获的异常
            e.printStackTrace();
          }
        }
      }
    };

    Thread t1 = new Thread(() -> {
      int c = 1 / 0;
    });
    threadPoolExecutor.execute(t1);
    Callable<Integer> callable = () -> 2 / 0;
    threadPoolExecutor.submit(callable);

    Thread newThread = new Thread(new Runnable() {
      @Override public void run() {
        System.out.println("===============================");
      }
    });
    newThread.setName("newThread!!");
    threadPoolExecutor.execute(newThread);
  }
}
